package com.udhtu.ii.lab2;

import static com.udhtu.ii.lab2.Function.*;

public class ThirdMethod {
    private double x;
    private double y;
    private double Xviv, Yviv, Inew, step;

    public ThirdMethod() {
        this.x = X_FIRST;
        this.y = Y_FIRST;
        Xviv = X_FIRST;
        Yviv = Y_FIRST;
        Inew = 1.0;
        step = STEP;
    }


    public void thirdMethod() {
        double Fx, Fy, Fdlina, S1, S2, fun, Fmin, Xnew, Ynew;

        fun = function(x, y);
        Fmin = fun;

        for (int i = 1; i <= N; i++) {
            Fx = 2 * x - 4;
            Fy = 2 * y - 2;
            Fdlina = Math.sqrt(Math.pow(Fx, 2) + Math.pow(Fy, 2));
            S1 = Fx / Fdlina;
            S2 = Fy / Fdlina;

            Xnew = x - step * S1;
            Ynew = y - step * S2;

            fun = function(Xnew, Ynew);
            if (fun < Fmin) {
                Fmin = fun;
                Inew = i;
                Xviv = Xnew;
                Yviv = Ynew;
            }
            x = Xnew;
            y = Ynew;
        }
    }

    public void compute() {
        this.thirdMethod();
        System.out.println("Минимум функции в точке : [" + Xviv + ";" + Yviv + "]");
        System.out.println("Значение функции: " + function(Xviv, Yviv));
        System.out.println("Количество итераций по = " + Inew);
    }

}
